;; exercise 1.6

;; new if

(define (new-if predicate then-clause else-clause)
   (cond (predicate then-clause)
         (else else-clause)))

(define (abs x)
  (if (< x 0) (- x) x))

(define (improve guess x)
   (average guess (/ x guess)))

(define (average x y)
  (/ (+ x y) 2))

(define (good-enough? guess x)
   (< (abs (- (square guess) x)) 0.001))
  
;; due to applicative order evaluation, the recursive call to sqrt-iter
;; will always be made, thus the program will never terminate
(define (sqrt-iter guess x)
   (new-if (good-enough? guess x)
           guess
           (sqrt-iter (improve guess x)
                      x)))

(define (sqrt x)
   (sqrt-iter 1.0 x))